package com.ipharma.gui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

import oracle.jdeveloper.layout.GridBagConstraints2;

import com.ipharma.McdError;
import com.ipharma.application.services.transaccion.Transaction;
import com.ipharma.config.Session;
import com.ipharma.dto.Product;

/**
 * A Swing-based panel class.
 * <P>
 * @author JorgE.H
 */
public class GUITransaction extends JPanel
{
  /**
   * Attributes
   */
  Transaction selectedProducts;
  GUIOperation myContainer;
  Session theSess;

  /**
   * Panel
   */
  JTextArea jDetailArea = new JTextArea();
  JScrollPane jDetailPane = new JScrollPane();
  JLabel jLabelTitle = new JLabel();
  GridBagLayout layoutBase = new GridBagLayout();

  /**
   * Constructs a new instance.
   */
  public GUITransaction( GUIOperation pContainer )
  {
    super();
    try
    {
      myContainer = pContainer;
      theSess = myContainer.theSess;
      jbInit();
    }
    catch (Exception e)
    {
      McdError.add( e );
    }
  }

  /**
   * Initializes the state of this instance.
   */
  private void jbInit() throws Exception
  {
    this.setLayout( layoutBase );
    this.setBorder( BorderFactory.createLineBorder( Color.black ) );
    this.setBackground( Colors.TSN_PNL );

    jLabelTitle.setText( Texts.TSN_TIT_PANEL );
    jLabelTitle.setForeground( Colors.TSN_PNL_TEXT );
    jLabelTitle.setHorizontalAlignment( SwingConstants.CENTER );
    jLabelTitle.setFont( Texts.fntTsnTitle );

    this.add( jLabelTitle, new GridBagConstraints2( 0, 0, 1, 1, 1.0, 0.0,
            GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets( 2, 2, 2, 2 ), 0, 15 ) );
    this.add( jDetailPane, new GridBagConstraints2( 0, 1, 1, 1, 1.0, 1.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets( 0, 2, 0, 2), 0, 200 ) );
    jDetailPane.getViewport().add( jDetailArea, null );
    jDetailArea.setEditable( false );
    jDetailArea.setFont( Texts.fntTsnDetail );
    jDetailArea.setRequestFocusEnabled( false );
  }

  /**
   * Adds an item to the transaction. Shows the transaction on screen.
   * @param pProd the product
   */
  public void addProduct( Product pProd, int pQty )
  {
    Product l_prod = (Product) pProd.clone();
    l_prod.setQuantity( pQty );
    if ( selectedProducts == null )
      selectedProducts = new Transaction( theSess );
    selectedProducts.addItem( l_prod );

    jDetailArea.setText( jDetailArea.getText() + pProd.getDescription() + " - "
        + pQty );
  }

  /**
   * Ends current transaction.
   */
  public void endTransaction( int pAlert )
  {
    boolean lb_alert = false;

    if ( selectedProducts != null )
      lb_alert = selectedProducts.end( pAlert );
    if ( lb_alert )
      jDetailArea.setText( jDetailArea.getText() + " - " + Texts.TSN_MSG_ALERT );

    jDetailArea.setText( jDetailArea.getText() + "\n" );
  }
}


